﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;

namespace EstateTrackerObjects.Collections
{
    public class BaseEstateUpgrades : ICollection<BaseEstateUpgrade>
    {

        private readonly ICollection<BaseEstateUpgrade> _estateUpgrades;
        public string connectionString { get; private set; }

        public BaseEstateUpgrades()
        {
            _estateUpgrades = new Collection<BaseEstateUpgrade>();
            connectionString = EstateTrackerObjects.Properties.Settings.Default.BeronisDBConnectionStringLive;
        }

        public void Load()
        {

            string sql = "SELECT ID FROM EstateUpgrades";
            var ds = new DataSet();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {                
                var adpt = new SqlDataAdapter(sql, conn);
                conn.Open();
                adpt.Fill(ds);
                conn.Close();
            }
            BaseEstateUpgrade eu;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                eu = new BaseEstateUpgrade();
                eu.load((int)dr[0]);
                this.Add(eu);
            }

        }

        #region "Interface objects"
        public void Add(BaseEstateUpgrade item)
        {
            _estateUpgrades.Add(item);            
        }

        public void Clear()
        {
            _estateUpgrades.Clear();
        }

        public bool Contains(BaseEstateUpgrade item)
        {
            return _estateUpgrades.Contains(item);
        }

        public void CopyTo(BaseEstateUpgrade[] array, int arrayIndex)
        {
            _estateUpgrades.CopyTo(array, arrayIndex);            
        }

        public int Count
        {
            get { return _estateUpgrades.Count(); }
        }

        public bool IsReadOnly
        {
            get { return _estateUpgrades.IsReadOnly; }
        }

        public bool Remove(BaseEstateUpgrade item)
        {
            return _estateUpgrades.Remove(item);
        }

        public IEnumerator<BaseEstateUpgrade> GetEnumerator()
        {
            return _estateUpgrades.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _estateUpgrades.GetEnumerator();
        }

        #endregion
    }
}
